package net.lgpage.util;

import java.util.List;

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

public class WifiUtil {

	// 定义几种加密方式，一种是WEP，一种是WPA，还有没有密码的情况
	public enum WifiCipherType {
		WEP, WPA, NOPASS, INVALID
	}
	
	public static boolean connect(WifiManager mng,String ssid){
		List<WifiConfiguration>list = mng.getConfiguredNetworks();
		for(WifiConfiguration cf:list){
			if(cf.SSID.equals("\""+ssid+"\"")){
				mng.enableNetwork(cf.networkId, true);
				return true;
			}
		}
		return false;
	}

	public static  WifiConfiguration createWifiInfo(String ssid, String passawrd,
			int paramInt, String type) {
		// 配置网络信息�?
		WifiConfiguration conf = new WifiConfiguration();
		// 设置配置网络属�??
		conf.allowedAuthAlgorithms.clear();
		conf.allowedGroupCiphers.clear();
		conf.allowedKeyManagement.clear();
		conf.allowedPairwiseCiphers.clear();
		conf.allowedProtocols.clear();

		if (type.equals("wifi")) { // wifi连接
			conf.SSID = ("\"" + ssid + "\"");
			// WifiConfiguration localWifiConfiguration2 = IsExsits(ssid);
			// if (localWifiConfiguration2 != null) {
			// mWifiManager.removeNetwork(localWifiConfiguration2.networkId); //
			// 从列表中删除指定的网络配置网�?
			// }
			if (paramInt == 1) { // 没有密码
				// localWifiConfiguration1.wepKeys[0] = "";
				// localWifiConfiguration1.allowedKeyManagement.set(0);
				// localWifiConfiguration1.wepTxKeyIndex = 0;

				conf.wepKeys[0] = "\"" + "\"";
				conf.allowedKeyManagement
						.set(WifiConfiguration.KeyMgmt.NONE);
				conf.wepTxKeyIndex = 0;

			} else if (paramInt == 2) { // �?单密�?
				conf.hiddenSSID = true;
				conf.wepKeys[0] = ("\"" + passawrd + "\"");
			} else { // wap加密
				conf.preSharedKey = ("\"" + passawrd + "\"");
				conf.hiddenSSID = true;
				conf.allowedAuthAlgorithms.set(0);
				conf.allowedGroupCiphers.set(2);
				conf.allowedKeyManagement.set(1);
				conf.allowedPairwiseCiphers.set(1);
				conf.allowedGroupCiphers.set(3);
				conf.allowedPairwiseCiphers.set(2);
			}
		} else {// "ap" wifi热点
			conf.SSID = ssid;
			conf.allowedAuthAlgorithms.set(1);
			conf.allowedGroupCiphers
					.set(WifiConfiguration.GroupCipher.CCMP);
			conf.allowedGroupCiphers
					.set(WifiConfiguration.GroupCipher.TKIP);
			conf.allowedGroupCiphers
					.set(WifiConfiguration.GroupCipher.WEP40);
			conf.allowedGroupCiphers
					.set(WifiConfiguration.GroupCipher.WEP104);
			conf.allowedKeyManagement.set(0);
			conf.wepTxKeyIndex = 0;
			if (paramInt == 1) { // 没有密码
				conf.wepKeys[0] = "";
				conf.allowedKeyManagement.set(0);
				conf.wepTxKeyIndex = 0;
			} else if (paramInt == 2) { // �?单密�?
				conf.hiddenSSID = true;// 网络上不广播ssid
				conf.wepKeys[0] = passawrd;
			} else if (paramInt == 3) {// wap加密
				conf.preSharedKey = passawrd;
				conf.allowedAuthAlgorithms.set(0);
				conf.allowedProtocols.set(1);
				conf.allowedProtocols.set(0);
				conf.allowedKeyManagement.set(1);
				conf.allowedPairwiseCiphers.set(2);
				conf.allowedPairwiseCiphers.set(1);
			}
		}
		return conf;
	}

	public static boolean baseConnect(WifiManager mng,String ssid,String password,WifiCipherType type){
		List<WifiConfiguration>list = mng.getConfiguredNetworks();
		for(WifiConfiguration cf:list){
			if(cf.SSID.equals("\""+ssid+"\"")){
				mng.removeNetwork(cf.networkId);
			}
		}
		WifiConfiguration conf = createWifiConfiguration(ssid, password, type, "wifi");
		int netID = mng.addNetwork(conf);
		mng.startScan();
		mng.updateNetwork(conf);
		mng.saveConfiguration();
		return mng.enableNetwork(netID, true);
	}
	
	public static boolean connect(WifiManager mng,String ssid,String password,WifiCipherType type){
		WifiConfiguration conf = createWifiConfiguration(ssid, password, type, "wifi");
		int netID = mng.addNetwork(conf);
		mng.startScan();
		mng.updateNetwork(conf);
		mng.saveConfiguration();
		return connect(mng, ssid);
	}
	/**
	 * WifiConfiguration.AuthAlgorthm 用来判断加密方法�? WifiConfiguration.GroupCipher
	 * 获取使用GroupCipher 的方法来进行加密�? WifiConfiguration.KeyMgmt 获取使用KeyMgmt 进行�?
	 * WifiConfiguration.PairwiseCipher 获取使用WPA 方式的加密�??
	 * WifiConfiguration.Protocol 获取使用哪一种协议进行加密�??
	 * wifiConfiguration.Status获取当前网络的状态�??
	 * 
	 * @param SSID
	 * @param Password
	 * @param Type
	 * @return
	 */
	public static WifiConfiguration createWifiConfiguration(String SSID, String Password,
			WifiCipherType Type, String wifi_type) {
		WifiConfiguration wc = new WifiConfiguration();
		wc.allowedAuthAlgorithms.clear();
		wc.allowedGroupCiphers.clear();
		wc.allowedKeyManagement.clear();
		wc.allowedPairwiseCiphers.clear();
		wc.allowedProtocols.clear();
		wc.SSID = "\"" + SSID + "\"";

		if (wifi_type.equals("wifi")) { // wifi连接
			if (Type == WifiCipherType.NOPASS) {
				System.out.println("没有密码的连�?============================");

				wc.wepKeys[0] = "\"" + "\"";// 没密码的
				wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
				wc.wepTxKeyIndex = 0;

			} else if (Type == WifiCipherType.WEP) {

				System.out.println("WEP密码的连�?============================");
				wc.wepKeys[0] = "\"" + Password + "\"";// wep加密方式
				wc.hiddenSSID = true;
				wc.allowedAuthAlgorithms
						.set(WifiConfiguration.AuthAlgorithm.SHARED);
				wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
				wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
				wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
				wc.allowedGroupCiphers
						.set(WifiConfiguration.GroupCipher.WEP104);
				wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
				wc.wepTxKeyIndex = 0;
				// System.out.println(wc.preSharedKey);
			} else if (Type == WifiCipherType.WPA) {

				System.out.println("WPA密码的连�?============================");
				wc.preSharedKey = "\"" + Password + "\"";// wpa加密方式
				wc.hiddenSSID = true;
				// 用来判断加密方法�?
				// 可�?�参数：LEAP只用于leap,
				// OPEN 被wpa/wpa2�?�?,
				// SHARED�?要一个静态的wep key

				wc.allowedAuthAlgorithms
						.set(WifiConfiguration.AuthAlgorithm.OPEN);
				// 用来判断加密方法。可选参数：CCMP,TKIP,WEP104,WEP40

				wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
				wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
				// WifiConfiguration.KeyMgmt 键管理机制（keymanagerment），使用KeyMgmt
				// 进行�?
				// 可�?�参数IEEE8021X,NONE,WPA_EAP,WPA_PSK
				wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
				// WifiConfiguration.PairwiseCipher 设置加密方式�?
				// 可�?�参�? CCMP,NONE,TKIP
				wc.allowedPairwiseCiphers
						.set(WifiConfiguration.PairwiseCipher.TKIP);
				wc.allowedPairwiseCiphers
						.set(WifiConfiguration.PairwiseCipher.CCMP);
				// WifiConfiguration.Protocol 设置�?种协议进行加密�??
				// 可�?�参�? RSN,WPA,
				wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // for
																			// WPA
				wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // for
																			// WPA2
				// wifiConfiguration.Status 获取当前网络的状态�??
			}
		} else {
			System.out.println("进入AP链接地址方式========================");
			wc.allowedAuthAlgorithms.set(1);
			wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
			wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
			wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
			wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
			wc.allowedKeyManagement.set(0);
			wc.wepTxKeyIndex = 0;
			if (Type == WifiCipherType.NOPASS) { // 没有密码
				System.out.println("AP无密码的连接============================");
				wc.wepKeys[0] = "";
				wc.allowedKeyManagement.set(0);
				wc.wepTxKeyIndex = 0;
			} else if (Type == WifiCipherType.WEP) { // �?单密�?
				System.out.println("APWEP密码的连�?============================");
				wc.hiddenSSID = true;// 网络上不广播ssid
				wc.wepKeys[0] = Password;
			} else if (Type == WifiCipherType.WPA) {// wap加密
				System.out.println("APWPA密码的连�?============================");
				wc.preSharedKey = Password;
				wc.allowedAuthAlgorithms.set(0);
				wc.allowedProtocols.set(1);
				wc.allowedProtocols.set(0);
				wc.allowedKeyManagement.set(1);
				wc.allowedPairwiseCiphers.set(2);
				wc.allowedPairwiseCiphers.set(1);
			}
			return wc;
		}
		return wc;
	}
	
}
